/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.core.log.writers
{
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import sweezy.core.log.LogLevel;
	import sweezy.core.log.LogWriter;

	public class HttpLogWriter extends LogWriter
	{

		private var _levelParameterName:String;

		private var _logParameterName:String;

		private var _method:String;

		private var _request:URLRequest;

		private var _url:String;

		private var _variables:URLVariables;

		public function HttpLogWriter()
		{
			_method = URLRequestMethod.POST;
			_levelParameterName = "level";
			_logParameterName = "log";
		}

		public function get levelParameterName():String
		{
			return _levelParameterName;
		}

		public function set levelParameterName(value:String):void
		{
			if (!value)
			{
				value = "level";
			}
			if (_levelParameterName === value)
			{
				return;
			}

			_levelParameterName = value;
			_variables = null;
		}

		public function get logParameterName():String
		{
			return _logParameterName;
		}

		public function set logParameterName(value:String):void
		{
			if (!value)
			{
				value = "log";
			}
			if (_logParameterName === value)
			{
				return;
			}

			_logParameterName = value;
			_variables = null;
		}

		public function get method():String
		{
			return _method;
		}

		public function set method(value:String):void
		{
			if (value)
			{
				value = value.toLocaleUpperCase();
			}
			if (value !== URLRequestMethod.POST && value !== URLRequestMethod.GET)
			{
				value = URLRequestMethod.POST;
			}

			if (value === _method)
			{
				return;
			}

			_method = value;
			_request = null;
		}

		public function get url():String
		{
			return _url;
		}

		public function set url(value:String):void
		{
			if (value === "")
			{
				value = null;
			}
			if (value === _url)
			{
				return;
			}

			_url = value;
			_request = null;
		}

		override protected function writeString(level:uint, name:String, logString:String):void
		{
			if (!url)
			{
				return;
			}

			if (_variables === null)
			{
				_variables = new URLVariables();
			}
			_variables[_logParameterName] = logString;
			_variables[_levelParameterName] = LogLevel.LOG_LEVEL_NAMES[level];

			if (_request === null)
			{
				_request = new URLRequest(_url);
				_request.method = _method;
			}
			_request.data = _variables;

			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, loaderHandler);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loaderHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, loaderHandler);
			loader.load(_request);
		}

		private function loaderHandler(e:Event):void
		{
			var loader:URLLoader = e.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE, loaderHandler);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loaderHandler);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, loaderHandler);

			if (e is ErrorEvent)
			{
				trace((e as ErrorEvent).text);
			}
		}
	}
}
